package com.guoq.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.guoq.pojo.ItemCat;
import com.guoq.pojo.ItemCatExample;
import com.guoq.pojo.Result;
import com.guoq.service.IItemCatService;


/**
 * 
 * <P>类名称:ItemCatController
 * <P>类描述: 类目 Controller
 * <P>创建人:郭强  
 * <P>修改人:XXX 
 * <P>修改时间:2017年7月20日 上午11:59:04
 * <P>修改备注: 
 * @version 1.0.0
 * Copyright(c)2017西部在线-版权所有
 */
@Controller
@RequestMapping("/itemcat")
public class ItemCatController {
	@Resource
	private IItemCatService itemcatService;
	
	@RequestMapping("/toadd")
	public String toIndex(HttpServletRequest request,Model model){
		return "/itemcat/add";
	}
	
	@RequestMapping("/toedit")
	public String toedit(HttpServletRequest request,Model model){
		return "/itemcat/edit";
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: getitemcatById
	 *<P>描述: 根据id获取类目信息
	 *@param request
	 *@param model
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/getItemCatById")
	@ResponseBody
	public ItemCat getitemcatById(HttpServletRequest request,Model model){
		int id = Integer.parseInt(request.getParameter("id"));
		ItemCat itemcat = this.itemcatService.getItemCatById(id);
		return itemcat;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: selectItemCatByPid
	 *<P>描述: 根据pid查询下级类目列表
	 *@param request
	 *@param model
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/selectItemCatByPid")
	@ResponseBody
	public List<ItemCat> selectItemCatByPid(HttpServletRequest request,Model model){
		
		int fid = Integer.parseInt(request.getParameter("fid"));
		List<ItemCat> list = this.itemcatService.selectItemCatByPid(fid);
		return list;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: list
	 *<P>描述: 获取类目列表
	 *@param request
	 *@param model
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/list")
	@ResponseBody
	public List<ItemCat> list(HttpServletRequest request,Model model){
		
		List<ItemCat> list = this.itemcatService.getItemCatList();
		return list;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: add
	 *<P>描述: 新增
	 *@param itemcat
	 *@return   
	 * @throws Exception 
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public Result add(ItemCat itemcat) throws Exception{
		
		Result result = this.itemcatService.createItemCat(itemcat);
		return result;
	} 
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: update
	 *<P>描述: 编辑
	 *@param itemcat
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public Result update(ItemCat itemcat) throws Exception{
		
		Result result = this.itemcatService.updateItemCat(itemcat);
		return result;
	} 
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: delete
	 *<P>描述:	删除 
	 *@param id
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/delete")
	@ResponseBody
	public Result delete(ItemCat record) throws Exception{
		
		Result result = this.itemcatService.delete(record);
		return result;
	}
	
	@RequestMapping("/pagelist")
	@ResponseBody
	public List<ItemCat> pagelist(ItemCatExample example){
		PageHelper.startPage(1, 3); // 核心分页代码
		List<ItemCat> list = this.itemcatService.selectByExample(example);
		return list;
	}
}
